package com.itheima.studentproject.service;

import com.itheima.studentproject.dao.StudentDao;
import com.itheima.studentproject.domain.Student;

public class StudentService {


    public boolean addStudent(Student student){
        StudentDao studentDao = new StudentDao();
        return studentDao.addStudent(student);
    }

    public int isExits(String id) {
        //调用dao，拿到所有容器数据
        StudentDao studentDao = new StudentDao();
        Student[] arr = studentDao.findAll();
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                if(id.equals(stu.getId())){
                    index = i;
                    break;
                }
            }
        }
        return index;
    }

    public Student[] findStudent() {
        StudentDao studentDao = new StudentDao();
        Student[] arr = studentDao.findAll();
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                return arr;
            }
        }
        return null;
    }

    public boolean updateStudent(Student stu) {
        StudentDao studentDao = new StudentDao();
        boolean flag = studentDao.updateStudent(stu);
        return flag;
    }

    public boolean deleteStudent(int index) {
        StudentDao studentDao = new StudentDao();
        boolean flag = studentDao.deleteStudent(index);
        return flag;
    }
}
